package com.waiter.moximoxi.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;


import com.waiter.moximoxi.R;
import com.waiter.moximoxi.bean.MainTableCategoryBean;
import com.waiter.moximoxi.constant.Constant;
import com.waiter.moximoxi.net.API;
import com.waiter.moximoxi.utils.JsonTwoUtils;


import java.util.ArrayList;
import java.util.List;

import butterknife.OnClick;
import cn.yhong.aframework.adapter.ZmAdapter;
import cn.yhong.aframework.adapter.ZmHolder;
import cn.yhong.aframework.dialog.BaseDialog;
import cn.yhong.aframework.listener.OkHttpSuccessListener;
import cn.yhong.aframework.listener.OnCallbackListener;
import cn.yhong.aframework.utils.MyOkHttpUtils;
import cn.yhong.aframework.utils.SpUtils;
import cn.yhong.aframework.view.IBaseView;

/**
 * Created by 17639 on 2020/10/14.
 */

public class ManagerAreaDialog extends BaseDialog {

    private ImageView dialog_manager_area_dis;
    private ListView manager_area_listview;
    private TextView area_confirm;

    private List<MainTableCategoryBean> menuAreaBeans;
    private ManagerAreaAdapter managerAreaAdapter;
    private List<String> ids = new ArrayList<>();

    public ManagerAreaDialog(Context context, OnCallbackListener onCallbackListener) {
        super(context, onCallbackListener);
    }

    @Override
    public Dialog initDialog(Context context) {
        View view = View.inflate(context, R.layout.dialog_manager_area, null);
        dialog_manager_area_dis = view.findViewById(R.id.dialog_manager_area_dis);
        manager_area_listview = view.findViewById(R.id.manager_area_listview);
        area_confirm = view.findViewById(R.id.area_confirm);

        dialog_manager_area_dis.setOnClickListener(this);
        area_confirm.setOnClickListener(this);

        menuAreaBeans = new ArrayList<>();
        managerAreaAdapter = new ManagerAreaAdapter(context, menuAreaBeans);
        manager_area_listview.setAdapter(managerAreaAdapter);

        return initMatchWrapDialog(view, context, Gravity.CENTER, android.R.style.Animation_InputMethod);
    }

    /**
     * 获取餐厅区域
     */
    public void showSetDatas() {
        MyOkHttpUtils.post_CallBack(API.table_category, null, API.getHeads(context), new OkHttpSuccessListener(
                (IBaseView) context, "餐厅区域", 2) {
            @Override
            public void onSuccessResponse(String response) {
                if (API.filterJson(response)) {
                    List<MainTableCategoryBean> mainTableCategoryBean = JsonTwoUtils.parseListJson(response, MainTableCategoryBean.class);
                    if (mainTableCategoryBean != null && mainTableCategoryBean.size() > 0) {
                        menuAreaBeans.clear();
                        ids.clear();
                        for (int i = 0; i < mainTableCategoryBean.size(); i++) {
                            if(mainTableCategoryBean.get(i).isChecked){
                                ids.add(mainTableCategoryBean.get(i).id);
                            }
                        }
                        menuAreaBeans.addAll(mainTableCategoryBean);
                        managerAreaAdapter.notifyDataSetChanged();
                        showDialog();
                    }
                }
            }

            @Override
            public void onErrorResponse() {

            }
        });
    }


    @OnClick({R.id.dialog_manager_area_dis})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_manager_area_dis:
                break;
            case R.id.area_confirm:
                onCallbackListener.onCallback(0, ids.toString());
                break;
        }
        dismissDialog();
    }

    private int[] area_iv = new int[]{R.drawable.manager_dialog_normal, R.drawable.manager_dialog_selected};

    class ManagerAreaAdapter extends ZmAdapter<MainTableCategoryBean> {

        public ManagerAreaAdapter(Context mContext, List<MainTableCategoryBean> itemList) {
            super(mContext, itemList);
        }

        @Override
        public void init(ZmHolder zmHolder, final MainTableCategoryBean mainTableCategoryBean, int position) {
            LinearLayout item_manager_ll = zmHolder.getView(R.id.item_manager_ll);
            ImageView item_manager_iv = zmHolder.getView(R.id.item_manager_iv);
            TextView item_manager_tv = zmHolder.getView(R.id.item_manager_tv);

            item_manager_tv.setText(mainTableCategoryBean.title);

            int user_id = (int) SpUtils.getData(context, Constant.USER_ID, 0);

            if (mainTableCategoryBean.admin_id != 0) {
                item_manager_iv.setImageResource(area_iv[0]);
                item_manager_ll.setAlpha(user_id == mainTableCategoryBean.admin_id ? 1.0f : 0.1f);
                item_manager_ll.setEnabled(user_id == mainTableCategoryBean.admin_id ? true : false);
            } else {
                item_manager_ll.setEnabled(true);
                item_manager_ll.setAlpha(1.0f);
                item_manager_iv.setImageResource(area_iv[0]);
            }

            item_manager_iv.setImageResource(mainTableCategoryBean.isChecked ? area_iv[1] : area_iv[0]);

            item_manager_ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mainTableCategoryBean.isChecked = !mainTableCategoryBean.isChecked;
                    if (mainTableCategoryBean.isChecked) {
                        ids.add(mainTableCategoryBean.id);
                    }else {
                        ids.remove(mainTableCategoryBean.id);
                    }
                    notifyDataSetChanged();
                }
            });
        }

        @Override
        public int setLayout() {
            return R.layout.item_dialog_manager_area;
        }
    }
}
